<script setup>

import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref, reactive, onMounted } from 'vue'
let categorys = reactive([]);
// {
//     "id": 3,
//     "categoryName": "美食",
//     "categoryAlias": "my",
//     "createTime": "2023-09-02 12:06:59",
//     "updateTime": "2023-09-02 12:06:59"
// },
// {
//     "id": 4,
//     "categoryName": "娱乐",
//     "categoryAlias": "yl",
//     "createTime": "2023-09-02 12:08:16",
//     "updateTime": "2023-09-02 12:08:16"
// },
// {
//     "id": 5,
//     "categoryName": "军事",
//     "categoryAlias": "js",
//     "createTime": "2023-09-02 12:08:33",
//     "updateTime": "2023-09-02 12:08:33"
// }


// 声明一个异步函数用于调用service中的接口
import { articleCategoryListService } from '@/api/article.js'
const articleCategoryList = async () => {
    let result = null;
    try {
        result = await articleCategoryListService();
        // 给对象赋值
        // 使用reactive做对象时, 不能直接用=赋值, 要使用object.assign
        Object.assign(categorys, result.data)
    } catch (error) {
        console.log(error);
    }
}
articleCategoryList();

//控制添加分类弹窗
const dialogVisible = ref(false)

//添加分类数据模型
let categoryModel = reactive({
    categoryName: '',
    categoryAlias: ''
})
//添加分类表单校验
const rules = {
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
    ],
    categoryAlias: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
    ]
}

// 点击确认按钮, 提交表单
import { addArticleCategoryService } from "@/api/article.js"
import { updateArticleCategoryService } from '@/api/article.js'
import { ElMessage } from 'element-plus';
let title_dialog = ref('添加分类');
const addCategory = async () => {
    let result = null;
    if (title_dialog.value == '添加分类') {
        try {
            result = await addArticleCategoryService(categoryModel)
            ElMessage({
                message: '添加分类成功',
                type: 'success',
            })
            dialogVisible.value = false;
            console.log(result.data)
            // 调用接口刷新数据
            articleCategoryList();
        } catch (error) {
            ElMessage.error('添加分类失败')
            console.log(error)
        }
    } else {
        try {
            result = await updateArticleCategoryService(categoryModel);
            ElMessage({
                message: '修改分类成功',
                type: 'success',
            })

            // 调用接口刷新数据
            articleCategoryList();
            dialogVisible.value = false;
        } catch (error) {
            ElMessage.error('修改分类失败')
            console.log(error)
        }
    }


}

// 点击修改, 修改已编辑的分类
// 显示修改弹窗

const showDialog_update = (row) => {
    dialogVisible.value = true;
    title_dialog.value = '修改分类';
    // row是当前按钮所在列的所有数据的对象(汇总)
    // 以实现数据的回显
    categoryModel.categoryName = row.categoryName;
    categoryModel.categoryAlias = row.categoryAlias;

    // 扩展id属性, 将来需要传递给后台
    categoryModel.id = row.id;
    console.log(categoryModel.categoryName)
}


// 点击删除, 删除分类
import { ElMessageBox } from 'element-plus'
import { deleteArticleCategoryService } from '@/api/article.js'
let deleteCategory = async (row) => {
    // 提示用户的确认框
    ElMessageBox.confirm(
        '确定要删除这个分类吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            // 点击确认后, 调用接口
            let result = await deleteArticleCategoryService(row.id);
            ElMessage({
                type: 'success',
                message: '删除成功',
            })

            // 调用接口刷新数据
            articleCategoryList();
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '用户取消了删除',
            })
        })
}




</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>文章分类</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible = true, title_dialog = '添加分类'">添加分类</el-button>
                </div>
            </div>
        </template>

        <el-table :data="categorys" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="分类名称" prop="categoryName"></el-table-column>
            <el-table-column label="分类别名" prop="categoryAlias"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog_update(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加/修改分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title_dialog" width="30%">
            <!-- 添加校验规则 :rule -->
            <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="分类别名" prop="categoryAlias">
                    <el-input v-model="categoryModel.categoryAlias" minlength="1" maxlength="15"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="addCategory()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>


    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>